package com.fundcrawler.web;// src/main/java/com/example/controller/BoardApiController.java

import com.fundcrawler.dto.Response;
import com.fundcrawler.entity.ConceptBoard;
import com.fundcrawler.service.ConceptBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BoardApiController {

    @Autowired
    private ConceptBoardService boardService;

    @GetMapping("/api/boards/crawlAllStockBoard")
    public Response crawlAllStockBoard() {

        boardService.fetchAndSaveAllBoards();
        return Response.message("ok");
    }

    /**
     * 获取所有概念板块数据
     * GET /api/boards
     * 支持按涨幅排序：?sort=changePercent,desc
     */
    @GetMapping("/api/boards")
    public ResponseEntity<List<ConceptBoard>> getAllBoards(
            @RequestParam(value = "sort", defaultValue = "changePercent,desc") String sort) {

        List<ConceptBoard> boards = boardService.getAllBoardsSorted(sort);
        return ResponseEntity.ok(boards);
    }

    /**
     * 获取最新更新时间
     */
    @GetMapping("/api/boards/latest")
    public ResponseEntity<String> getLatestUpdateTime() {
        return ResponseEntity.ok(boardService.getLatestTime());
    }
}
